package com.ctshk.rpc.ship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.ship.entity.ShipInitializationSettings;
import com.ctshk.rpc.ship.mapper.ShipInitializationSettingsMapper;
import com.ctshk.rpc.ship.req.ShipInitializationSettingsSaveReq;
import com.ctshk.rpc.ship.service.IShipInitializationSettingsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

/**
 * <p>
 * 船票api初始化设置表 服务实现类
 * </p>
 *
 * @author 刘蕾
 * @since 2021-03-26
 */
@DubboService
public class ShipInitializationSettingsServiceImpl extends ServiceImpl<ShipInitializationSettingsMapper, ShipInitializationSettings> implements IShipInitializationSettingsService {

    @Autowired
    ShipInitializationSettingsMapper shipInitializationSettingsMapper;


    @Override
    public Result save(ShipInitializationSettingsSaveReq shipInitializationSettingsSaveReq, TokenUser user) {
        Long id = shipInitializationSettingsSaveReq.getId();
        ShipInitializationSettings shipInitializationSettings = (ShipInitializationSettings) detail(shipInitializationSettingsSaveReq).getData();
        if(shipInitializationSettings == null){
            shipInitializationSettings = new ShipInitializationSettings();
        }
        BeanUtils.copyProperties(shipInitializationSettingsSaveReq,shipInitializationSettings);
        shipInitializationSettings.setId(SnowflakeIdWorker.nextId());
        shipInitializationSettings.setBookingPrice(shipInitializationSettingsSaveReq.getBookingPrice().doubleValue());
        shipInitializationSettings.setRefundFeePrice(shipInitializationSettingsSaveReq.getRefundFeePrice().doubleValue());
        shipInitializationSettings.setCreateId(user.getId());
        shipInitializationSettings.setGmtCreate(LocalDateTime.now());
        return Result.success(id != null ? shipInitializationSettingsMapper.updateById(shipInitializationSettings) :
                shipInitializationSettingsMapper.insert(shipInitializationSettings));
    }

    @Override
    public Result detail(ShipInitializationSettingsSaveReq shipInitializationSettingsSaveReq) {
        QueryWrapper<ShipInitializationSettings> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ShipInitializationSettings::getSupplierId,shipInitializationSettingsSaveReq.getSupplierId());
        ShipInitializationSettings shipInitializationSettings = shipInitializationSettingsMapper.selectOne(queryWrapper);
        return Result.success(shipInitializationSettings);
    }
}
